﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LasRendering
{
    public class QuadTreeVertexIndexLink : IEnumerable<QuadTreeVertexIndexLinkNode>
    {
        public QuadTreeVertexIndexLinkNode First
        {
            private set;
            get;
        }

        public QuadTreeVertexIndexLinkNode Last
        {
            private set;
            get;
        }

        public int Count
        {
            private set;
            get;
        }

        public void Add(int index)
        {
            this.Count++;
            var last = this.Last;
            if (last != null && (index - last.EndIndex) == 1)
            {
                last.EndIndex = index;
            }
            else
            {
                this.Add(new QuadTreeVertexIndexLinkNode(index));
            }
        }

        public void Add(int index, int count)
        {
            if (count <= 0)
            {
                return;
            }

            this.Count += count;
            var last = this.Last;
            if (last != null && (index - last.EndIndex) == 1)
            {
                last.EndIndex = index + count - 1;
            }
            else
            {
                this.Add(new QuadTreeVertexIndexLinkNode(index, count));
            }
        }

        private void Add(QuadTreeVertexIndexLinkNode node)
        {
            if (this.First == null)
            {
                this.First = node;
            }
            if (this.Last != null)
            {
                this.Last.Next = node;
            }
            node.Next = null;
            this.Last = node;
        }

        public void Clear()
        {
            this.Count = 0;
            this.First = null;
            this.Last = null;
        }

        #region IEnumerable<QuadTreeVertexIndexLinkNode> section

        public class Enumerator : IEnumerator<QuadTreeVertexIndexLinkNode>
        {
            private QuadTreeVertexIndexLinkNode First = new QuadTreeVertexIndexLinkNode(0);

            public Enumerator(QuadTreeVertexIndexLinkNode first)
            {
                this.First.Next = first;
                this.Reset();
            }

            public QuadTreeVertexIndexLinkNode Current
            {
                private set;
                get;
            }

            public void Dispose()
            {
            }

            object System.Collections.IEnumerator.Current
            {
                get 
                { 
                    return this.Current;
                }
            }

            public bool MoveNext()
            {
                if (this.Current == null)
                {
                    return false;
                }

                this.Current = this.Current.Next;
                return this.Current != null;
            }

            public void Reset()
            {
                this.Current = this.First;
            }
        }

        public IEnumerator<QuadTreeVertexIndexLinkNode> GetEnumerator()
        {
            return new Enumerator(this.First);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion

        public override string ToString()
        {
            return string.Format("Count = {0}", this.Count);
        }
    }
}
